/**
 * @file   bufferguard-inl.h
 * @Author Alexandre Chassany (alexandre.chassany@gmail.com)
 * @date   February 2011
 * @brief  BufferGuard template implementation and specialisation
 *
 * Poum-tchak is a free sound manipulation and synthesis, 
 * real-time and interactive music-clip rendering.
 *
 * Copyright (C) 2011  Chassany Alexandre and Gonzalez Antoine.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * he Free Software Foundation, either version 3 of the License, or
 * at your option) any later version.
 *
 * is program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef _ELIXIR_BUFFERGUARD_INL_H_
#define _ELIXIR_BUFFERGUARD_INL_H_

template<class T>
inline BufferGuard<T>::BufferGuard(IBufferBase* buffer)
:buffer_(buffer)
{
}

template<class T>
inline T* BufferGuard<T>::lock(unsigned long offset, unsigned long size, unsigned long  flags)
{
  return reinterpret_cast<T*>(buffer_->lock(offset*sizeof(T),size*sizeof(T),flags));
}

template<class T>
inline void BufferGuard<T>::unlock()
{
  buffer_->unlock();
}

template<class T>
inline void BufferGuard<T>::fill(const T* data, std::size_t count)
{
  // Todo add an assert on the data
  T* mydata = lock();
  std::copy(data, data+count, mydata);
  unlock();
}

template<class T>
inline void BufferGuard<T>::release()
{
  buffer_ = NULL;
}

template<class T>
inline const IBufferBase* BufferGuard<T>::getBuffer() const
{
  return buffer_.get();
}

template<class T>
inline unsigned long BufferGuard<T>::getCount() const
{
  return buffer_->count_;
}

#endif // _ELIXIR_BUFFERGUARD_INL_H_
